﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace B12_Ex03_Eran_053094363_Assaf_036722999
{
    public class SearchingLabel : IObserver
    {
        private Label m_label;
        private ProgressBar m_ProgressBar;
        private Timer m_Timer = new Timer();

        public SearchingLabel()
        {
            // Set the Label
            m_label = new System.Windows.Forms.Label();
            m_label.AutoSize = true;
            m_label.Location = new System.Drawing.Point(220, 340);
            m_label.Name = "labelSearching";
            m_label.Size = new System.Drawing.Size(0, 13);
            m_label.TabIndex = 23;
            m_label.Text = string.Empty;

            // Set the Progress bar
            m_ProgressBar = new ProgressBar();
            m_ProgressBar.Name = "progressBar";
            m_ProgressBar.Margin = new Padding(5);
            m_ProgressBar.Size = new System.Drawing.Size(100, 20);
            m_ProgressBar.Minimum = 0;
            m_ProgressBar.Maximum = 100;
            m_ProgressBar.Step = 1;
            m_ProgressBar.Value = 0;
            m_ProgressBar.Visible = false;

            // Set the Timer for Incrementing the Progress abr
            initializeMyTimer();
        }

        public void Update(ISearchSubject i_ChangedSubjectState)
        {
            if (m_label.Text.Equals(string.Empty))
            {
                // Start the timer.
                m_Timer.Enabled = true;
                m_Timer.Start();

                // Set the Label Text
                m_label.Text = "Searching...";
                m_label.Update();

                // Set the Progress bar
                m_ProgressBar.Visible = true;
                m_ProgressBar.Location = new System.Drawing.Point(m_label.Location.X + m_label.Size.Width, m_label.Location.Y);
                m_ProgressBar.Update();
                m_ProgressBar.Refresh();
            }
            else
            {
                m_label.Text = string.Empty;
                m_label.Update();
                m_ProgressBar.Visible = false;
                m_Timer.Stop();
            }
        }

        public void Add(Form m_Form)
        {
            m_Form.Controls.Add(m_label);
            m_Form.Controls.Add(m_ProgressBar);
        }

        private void initializeMyTimer()
        {
            // Set the interval for the timer.
            m_Timer.Interval = 25;

            // Connect the Tick event of the timer to its event handler.
            m_Timer.Tick += new EventHandler(increaseProgressBar);
        }

        private void increaseProgressBar(object sender, EventArgs e)
        {
            // Increment the value of the ProgressBar a value of one each time.
            m_ProgressBar.Increment(1);
            
            // Determine if we have completed by comparing the value of the Value property to the Maximum value.
            if (m_ProgressBar.Value == m_ProgressBar.Maximum)
            {
                // Stop the timer.
                m_Timer.Stop();
            }
        }
    }
}
